2059. 转化数字的最小运算数
为保证权益,题目请参考 2059. 转化数字的最小运算数(From LeetCode).
解决方案1
Python
python
# 2059. 转化数字的最小运算数
# https://leetcode-cn.com/problems/minimum-operations-to-convert-number/
################################################################################
from typing import List
class Solution:
def minimumOperations(self, nums: List[int], start: int, goal: int) -> int:
x = start
waitToCalculate = [x]
calculateTimes = 1
visited = set()
while len(waitToCalculate)!=0:
nextWaitToCalculate = set()
for xx in waitToCalculate:
if xx in visited:
continue
visited.add(xx)
for num in nums:
t = [xx + num, xx - num , xx ^ num]
for tmp in t:
if tmp == goal:
return calculateTimes
if 0 <= tmp <= 1000:
nextWaitToCalculate.add(tmp)
waitToCalculate = nextWaitToCalculate
calculateTimes += 1
return -1
################################################################################
if __name__ == "__main__":
solution = Solution()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34